package socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Server2 {
    private ServerSocket serverSocket;
    public Server2(){
        try {
            System.out.println("正在启动服务器");
            serverSocket=new ServerSocket(8877);
            System.out.println("服务器启动完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void satay()  {
        try {
            while (true) {
                System.out.println("等待客服端连接");
                Socket socket = serverSocket.accept();
                System.out.println("一个客服端连接成功");
                InputStream ips = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(ips, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);
                String ling;
                while ((ling = br.readLine()) != null) {
                    System.out.println("客服端说：" + ling);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server2 server2=new Server2();
        server2.satay();
    }
}
